package com.surekam.mnai.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.surekam.mnai.model.MNAIUser;
import com.surekam.mnai.util.StringUtil;

@Component
public class UserDao extends BaseDao {
	
	private static Logger logger = LoggerFactory.getLogger(UserDao.class);

	private UserMapper userMapper = new UserMapper();

	/**
	 * User 类与表T_USER的映射关系
	 * 
	 * @author yangy
	 * 
	 */
	private class UserMapper implements RowMapper<MNAIUser> {
		@Override
		public MNAIUser mapRow(ResultSet rs, int rowNum) throws SQLException {
			MNAIUser su = new MNAIUser();
			su.setId(rs.getLong("ID"));
			String time = rs.getTimestamp("TIME") == null ? "" : rs.getTimestamp("TIME").toString();
			su.setTime(time);
			su.setUserNumber(rs.getString("USER_NUMBER"));
			su.setUserPassword(rs.getString("USER_PASSWORD"));
			su.setUserState(rs.getString("USER_STATE"));
			su.setName(rs.getString("NAME"));
			return su;
		}
	}

	/**
	 * 
	 * @param user
	 */
	public void insert(MNAIUser user) {
		if (user == null) {
			throw new IllegalArgumentException("User实例不能为null..");
		}

		logger.info("插入新记录到User表中...");
		String insertSql = "INSERT INTO T_USER(USER_NUMBER," + "USER_PASSWORD,"
				+ "USER_STATE," + "TIME," + "NAME," + ") VALUES(?,?,?,?,?,?,?,?)";
		this.jdbcTemplate.update(insertSql, user.getUserNumber(),
				user.getUserPassword(), user.getUserState(),
				user.getTime());
		user.getName();

		logger.info("插入新记录到User表中...");
	}

	/**
	 * 更新操作
	 * 
	 * @param survey
	 *            survey不能为null且id不能为null,
	 */
	public void update(MNAIUser user) {
		if (user == null) {
			throw new IllegalArgumentException("User实例不能为null..");
		}

		if (user.getId() == null) {
			throw new IllegalArgumentException("User实例的id为空，无法完成更新..");
		}

		logger.info("更新User表中的记录：id=" + user.getId());
		String updateSql = "UPDATE T_USER SET " + "USER_NUMBER=?, " + "USER_PASSWORD=?, "
				+ "USER_STATE=?, " + "TIME=?, " + "NAME=?, " + "WHERE ID=?";
		this.jdbcTemplate.update(updateSql, user.getUserNumber(),
				user.getUserPassword(), user.getUserState(),
				user.getTime(), user.getName(), user.getId());

		logger.info("更新User表中的记录：id=" + user.getId() + "成功..");
	}

	/**
	 * 更新查勘员当前状态
	 * 
	 * @param userNumber
	 * @param state
	 */
	public void updateState(String userNumber, int state) {
		logger.info("更新User表中的记录：userNumber=" + userNumber);
		String updateSql = "UPDATE T_USER SET " + "USER_STATE=? " + "WHERE USER_NUMBER=?";
		this.jdbcTemplate.update(updateSql, state, userNumber);
		logger.info("更新User表中的记录：userNumber=" + userNumber + "成功…");
	}

	/**
	 * 根据id删除User中的一条记录
	 * 
	 * @param id
	 */
	public void deleteById(Long id) {
		if (id == null) {
			throw new IllegalArgumentException("id为空，无法根据id删除User表中的记录..");
		}

		logger.info("删除User表中的记录：id=" + id);

		String deleteSql = "DELETE T_USER WHERE ID=? ";
		this.jdbcTemplate.update(deleteSql, id);
		logger.info("删除User表中的记录：id=" + id + "成功..");
	}

	/**
	 * 根据id查出User一条记录
	 * 
	 * @param id
	 * @return
	 */
	public MNAIUser queryById(Long id) {
		String querySql = "SELECT * FROM T_USER WHERE ID= ?";
		return this.jdbcTemplate.queryForObject(querySql, userMapper, id);
	}

	/**
	 * 通过工号查出User一条记录
	 * 
	 * @param userId
	 * @return
	 */
	public MNAIUser queryBYUSERNUMBER(String userNumber) {
		String querySql = "SELECT * FROM T_USER WHERE USER_NUMBER LIKE '%?%'";
		return this.jdbcTemplate.queryForObject(querySql, userMapper, userNumber);
	}

	/**
	 * 没考虑到分页的情况
	 * 
	 * @param name
	 * @return
	 */
	public List<MNAIUser> queryBYName(String name) {
		if (StringUtil.isEmpty(name)) {
			throw new IllegalArgumentException("因没暂时没做分页处理，所以参数不能为空..");
		}
		String querySql = "SELECT * FROM T_USER WHERE NAME LIKE '%?%'";
		return this.jdbcTemplate.query(querySql, userMapper, name);
	}

	/**
	 * 没考虑到分页的情况
	 * 
	 * @param place
	 * @return
	 */
	public List<MNAIUser> queryBYUSER_STATE(String userState) {
		if (StringUtil.isEmpty(userState)) {
			throw new IllegalArgumentException("因没暂时没做分页处理，所以参数不能为空..");
		}
		String querySql = "SELECT * FROM T_USER WHERE USER_STATE LIKE '%?%'";
		return this.jdbcTemplate.query(querySql, userMapper, userState);
	}

	/**
	 * 用户登陆
	 * 
	 * @param usernumber
	 * @param password
	 * @return
	 */
	public MNAIUser loginByUserNumberAndPassword(String usernumber, String password) {
		String querySql = "select * from T_USER where USER_NUMBER=? and USER_PASSWORD=?";
		List<MNAIUser> userList = this.jdbcTemplate.query(querySql, userMapper, usernumber,
				password);
		if (userList != null && userList.size() > 0) {
			return userList.get(0);
		} else {
			return null;
		}
	}

	public MNAIUser loginByUserNumberAndPassword(String usernumber) {
		String querySql = "select * from T_USER where USER_NUMBER=? ";
		List<MNAIUser> userList = this.jdbcTemplate.query(querySql, userMapper, usernumber);
		if (userList != null && userList.size() > 0) {
			return userList.get(0);
		} else {
			return null;
		}
	}
}
